package com.szy.douyou_gateway.web;

import com.szy.douyou_gateway.config.AccountProfile;
import com.szy.douyou_gateway.config.ResultDTO;
import com.szy.douyou_gateway.dao.Friend;
import com.szy.douyou_gateway.service.FriendService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 交友controller
 * @author sunziyan
 * @date 2022/4/19
 */
@RestController
@RequestMapping("/friend")
public class FriendController {
    @Resource
    private FriendService friendService;

    @RequiresAuthentication
    @PostMapping("/saveChatHistory")
    public ResultDTO saveChatHistory(@RequestBody Friend friend){
        Subject subject = SecurityUtils.getSubject();
        AccountProfile principal = (AccountProfile) subject.getPrincipal();
        if (!principal.getUserId().equals(friend.getUserId())) {
            return ResultDTO.fail("无权限保存聊天记录");
        }

        return friendService.saveChatHistory(friend);
    }
    @RequiresAuthentication
    @GetMapping("/getChatHistory/{userId}/{friendId}")
    public ResultDTO getChatHistory(@PathVariable("userId")Long userId,@PathVariable("friendId")Long friendId){
        Subject subject = SecurityUtils.getSubject();
        AccountProfile principal = (AccountProfile) subject.getPrincipal();
        if (!principal.getUserId().equals(userId)) {
            return ResultDTO.fail("无权限查看聊天记录");
        }
        Friend friend = new Friend();
        friend.setFriendId(friendId);
        friend.setUserId(userId);
        return friendService.getChatHistory(friend);
    }

    /**
     * 添加好友
     * @param userId
     * @param friendId
     * @return
     */
    @RequiresAuthentication
    @GetMapping("/addFriend/{userId}/{friendId}")
    public ResultDTO addFriend(@PathVariable("userId")Long userId,@PathVariable("friendId")Long friendId){
        Subject subject = SecurityUtils.getSubject();
        AccountProfile principal = (AccountProfile) subject.getPrincipal();
        if (!principal.getUserId().equals(userId)) {
            return ResultDTO.fail("无权限添加好友");
        }
        Friend friend = new Friend();
        friend.setFriendId(friendId);
        friend.setUserId(userId);
        //去数据库查看是否已添加过好友，如果没有，添加两条记录
        if(friendService.getChatHistory(friend).getSuccess() != false){
            return ResultDTO.fail("你已经添加该好友！");
        }
        //添加好友
        return friendService.insertFriend(friend);

    }

    /**
     * 获取好友列表
     * @param userId
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequiresAuthentication
    @GetMapping("/getMyFriend/{userId}/{pageNo}/{pageSize}")
    public ResultDTO getMyFriend(@PathVariable("userId")Long userId,@PathVariable("pageNo")int pageNo,@PathVariable("pageSize")int pageSize){

        return friendService.getMyFriend(userId,pageNo,pageSize);
    }

    @RequiresAuthentication
    @GetMapping("/deleteOne/{userId}/{friendId}")
    public ResultDTO deleteOne(@PathVariable("userId")Long userId,@PathVariable("friendId")Long friendId){
        Subject subject = SecurityUtils.getSubject();
        AccountProfile principal = (AccountProfile) subject.getPrincipal();
        if (!principal.getUserId().equals(userId)) {
            return ResultDTO.fail("无权查看好友列表");
        }
        return friendService.deleteFriend(userId,friendId);
    }

}
